package com.itljm.cxrMarket.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itljm.cxrMarket.common.PageResult;
import com.itljm.cxrMarket.common.Result;
import com.itljm.cxrMarket.entity.HygieneInspectionRecords;
import com.itljm.cxrMarket.service.HygieneInspectionRecordsService;
import com.itljm.cxrMarket.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author ljm
 * @date 2025/5/17 14:23
 */
@RestController
@RequestMapping("/hygieneInspectionRecords")
public class HygieneInspectionRecordsController {
    @Autowired
    private HygieneInspectionRecordsService hygieneInspectionRecordsService;

    // 分页查询
    @RequestMapping("/info")
    public PageResult<HygieneInspectionRecords> findHygieneInspectionRecordsByPage(
            Integer page, Integer limit, String inspectorId, String merchantId,
            String startInspectionTime, String endInspectionTime, String overallResult
    ) {
        if (page == null) {
            page = 0;
        }
        if (limit == null) {
            limit = 15;
        }
        Page<HygieneInspectionRecords> infoPage = new Page<>(page,limit);
        QueryWrapper<HygieneInspectionRecords> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(merchantId)) {
            wrapper.eq("hir.merchant_id", merchantId);
        }
        if (StringUtils.isNotEmpty(inspectorId)) {
            wrapper.eq("hir.inspector_id", inspectorId);
        }
        if (StringUtils.isNotEmpty(startInspectionTime) && StringUtils.isNotEmpty(endInspectionTime)) {
            wrapper.ge("hir.inspection_time", startInspectionTime).le("hir.inspection_time", endInspectionTime);
        }
        if (StringUtils.isNotEmpty(overallResult)) {
            wrapper.like("hir.overall_result", overallResult);
        }
        List<HygieneInspectionRecords> list = hygieneInspectionRecordsService.findInspectionRecordsByPage(infoPage, wrapper);
        return new PageResult<>(list,infoPage.getTotal());
    }

    // 根据id查询
    @GetMapping("{id}")
    public Result findHygieneInspectionRecordsById(@PathVariable String id) {
        HygieneInspectionRecords info = hygieneInspectionRecordsService.findInspectionRecordsById(id);
        return info != null ? Result.ok(info) : Result.fail("记录不存在");
    }

    // 新增
    @PostMapping
    public Result addHygieneInspectionRecords(@RequestBody HygieneInspectionRecords info) {
        info.setCreateTime(new Date());
        info.setUpdateTime(new Date());
        return hygieneInspectionRecordsService.save(info) ? Result.ok() : Result.fail("新增失败");
    }

    // 根据id删除
    @DeleteMapping("{id}")
    public Result deleteHygieneInspectionRecordsById(@PathVariable String id) {
        return hygieneInspectionRecordsService.removeById(id) ? Result.ok() : Result.fail("删除失败");
    }

    // 根据id修改
    @PutMapping
    public Result updateHygieneInspectionRecords(@RequestBody HygieneInspectionRecords info) {
        return hygieneInspectionRecordsService.updateById(info) ? Result.ok() : Result.fail("修改失败");
    }
}
